package com.visikard.ui.fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;

import com.visikard.ui.R;
import com.visikard.ui.activity.RedemptionActivity;

public class SlideOpenCameraFragment extends BaseFragment {

	private LinearLayout lyCodeAbow, lyCodeBelow;
	
	private Animation swipeAnimationTop = null;
	private Animation swipeAnimationDown = null;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View parent = inflater.inflate(R.layout.slide_open_camera,
				container, false);
		innitControl(parent);
		return parent;
	}
	
	private void innitControl(View parent) {

		lyCodeAbow = (LinearLayout) parent.findViewById(R.id.layout_scancode_abow);
		lyCodeBelow = (LinearLayout) parent.findViewById(R.id.layout_scancode_below);
		
		swipeAnimationTop = AnimationUtils.loadAnimation(getActivity(),
				R.anim.slide_kard_out_to_top);
		swipeAnimationTop.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				lyCodeAbow.setVisibility(View.GONE);
			}
		});
		
		swipeAnimationDown = AnimationUtils.loadAnimation(getActivity(),
				R.anim.slide_kard_out_to_down);
		swipeAnimationDown.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				lyCodeBelow.setVisibility(View.GONE);
				((RedemptionActivity)getActivity()).mainPutFragment(new ScanCodeFragment(), "ScanCodeFragment");
			}
		});
		
		new LockKeyPadOperation().execute("");
	}
	
	private class LockKeyPadOperation extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			for (int i = 0; i < 2; i++) {
				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}

			return "Executed";
		}

		@Override
		protected void onPostExecute(String result) {
			lyCodeAbow.startAnimation(swipeAnimationTop);
			lyCodeBelow.startAnimation(swipeAnimationDown);
		}

		@Override
		protected void onPreExecute() {
		}

		@Override
		protected void onProgressUpdate(Void... values) {
			
		}
	}

}
